﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

namespace Department.admin.Content
{
    public partial class EditDocuments : System.Web.UI.Page
    {
        const int MAX_FILE_SIZE = AdminVariablesSingleton.MAX_FILE_SIZE;
        private string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            if (Request.Params["ID"] == null) // Redirect user if the page is loaded without params
            {
                Response.Redirect("/admin/Content/ViewAllDocuments.aspx");
            }

            this.Page.Title = "Редакция на документ";

            AdminVariablesSingleton.configureCKEditor(ref newsTextBox);

            string heading = "";
            string text = "";
            bool isHidden = false;
            string id = Request.Params["ID"];
            if (!Page.IsPostBack)
            {
                if (!(id == null))
                {
                    SqlConnection conn = new SqlConnection(strConnString);

                    try
                    {
                        conn.Open();

                        //Gets the news heading
                        string sqlHeading = "SELECT ID, Name, Description, IsHidden FROM Documents WHERE Documents.ID = @ID";
                        SqlCommand comHeading = new SqlCommand(sqlHeading, conn);
                        comHeading.Parameters.AddWithValue("@ID", id);

                        SqlDataReader dataReader = comHeading.ExecuteReader();
                        
                        while (dataReader.Read())
                        {
                            heading = dataReader.GetString(1);
                            text = dataReader.GetString(2);
                            isHidden = dataReader.GetBoolean(3);
                           
                        }

                        headingTextBox.Text = heading;
                        newsTextBox.Text = text;
                        checkboxActive.Checked = !isHidden;
                    }
                    catch (SqlException sqle)
                    {
                        Response.Redirect("/admin/ErrorPage.aspx?type=sql&err=" + sqle.Message);
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("/admin/ErrorPage.aspx?type=1&err=" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }

                }

            }

        }

        protected void buttonSubmit_Click(object sender, EventArgs e)
        {
            
            string newsID = Request.Params["ID"];
                       
            SqlConnection conn = new SqlConnection(strConnString);
            try
            {
                conn.Open();
                string sqlUpdate = "UPDATE Documents SET Name=@Heading, Description=@Text, Date = @AcademicYear, IsHidden=@IsHidden  WHERE ID = @ID";
                SqlCommand comUpdate = new SqlCommand(sqlUpdate, conn);
                string meca = headingTextBox.Text;
                comUpdate.Parameters.AddWithValue("@ID", Request.Params["ID"]);
                comUpdate.Parameters.AddWithValue("@Heading", headingTextBox.Text);
                comUpdate.Parameters.AddWithValue("@Text", newsTextBox.Text);
                comUpdate.Parameters.AddWithValue("@AcademicYear", DateTime.Now);
                comUpdate.Parameters.AddWithValue("@IsHidden", !checkboxActive.Checked);


                comUpdate.ExecuteNonQuery();
                Response.Redirect("~/admin/Content/ViewAllDocuments.aspx");
            }
            catch (SqlException sqle)
            {
                Response.Redirect("/admin/ErrorPage.aspx?type=sql&err=" + sqle.Message);
            }
            catch (Exception ex)
            {
                Response.Redirect("/admin/ErrorPage.aspx?type=1&err=" + ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
     
        
       

    }

}
